﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;

[RequireComponent(typeof(Model))]
public class ModelEmission : MonoBehaviour, IEditable, ICustomSerialization
{
    [CompilerGenerated]
    private static Func<Color32, Color32> <>f__am$cache6;
    [CompilerGenerated]
    private static Func<IGrouping<Color32, Color32>, Color32> <>f__am$cache7;
    [CompilerGenerated]
    private static Func<Color32, string> <>f__am$cache8;
    [CompilerGenerated]
    private static Func<string, <>__AnonType0<bool, Color32>> <>f__am$cache9;
    [CompilerGenerated]
    private static Func<<>__AnonType0<bool, Color32>, bool> <>f__am$cacheA;
    [CompilerGenerated]
    private static Func<<>__AnonType0<bool, Color32>, Color32> <>f__am$cacheB;
    private Color32[] blackColors;
    public List<Color32> emissionIndexColors;
    private Texture2D emissionTex;
    private Color32[] groupedMainTexColors;
    private Color32[] mainTexColors;
    private static Texture2D whiteTex;

    public void OnDeserialize(string dataString)
    {
        try
        {
            Data data = JsonUtility.FromJson<Data>(dataString);
            if (<>f__am$cache9 == null)
            {
                <>f__am$cache9 = delegate (string x) {
                    Color color;
                    return new <>__AnonType0<bool, Color32>(ColorUtility.TryParseHtmlString(x, out color), color);
                };
            }
            if (<>f__am$cacheA == null)
            {
                <>f__am$cacheA = x => x.success;
            }
            if (<>f__am$cacheB == null)
            {
                <>f__am$cacheB = x => x.color;
            }
            this.emissionIndexColors = data.emissionIndexHexColors.Select<string, <>__AnonType0<bool, Color32>>(<>f__am$cache9).Where<<>__AnonType0<bool, Color32>>(<>f__am$cacheA).Select<<>__AnonType0<bool, Color32>, Color32>(<>f__am$cacheB).ToList<Color32>();
        }
        catch (Exception exception)
        {
            Debug.LogException(exception);
        }
        if ((this.emissionIndexColors != null) && (this.emissionIndexColors.Count > 0))
        {
            this.Refresh();
        }
    }

    public void OnEditorGUI()
    {
        if (this.emissionTex == null)
        {
            this.Refresh();
        }
        GUILayout.BeginHorizontal(new GUILayoutOption[0]);
        for (int i = 0; i < this.groupedMainTexColors.Length; i++)
        {
            Color32 item = this.groupedMainTexColors[i];
            Color color = GUI.color;
            GUI.color = (Color) item;
            if ((i % 10) == 0)
            {
                GUILayout.EndVertical();
                GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            }
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(10f), GUILayout.Height(10f) };
            if (GUILayout.Button(whiteTex, options))
            {
                if (!this.emissionIndexColors.Contains(item))
                {
                    this.emissionIndexColors.Add(item);
                }
                else
                {
                    this.emissionIndexColors.Remove(item);
                }
                this.Refresh();
            }
            GUI.color = color;
        }
        GUILayout.EndVertical();
    }

    public string OnSerialize()
    {
        if ((this.emissionIndexColors == null) || (this.emissionIndexColors.Count == 0))
        {
            return null;
        }
        Data data = new Data();
        if (<>f__am$cache8 == null)
        {
            <>f__am$cache8 = x => "#" + ColorUtility.ToHtmlStringRGB((Color) x);
        }
        data.emissionIndexHexColors = this.emissionIndexColors.Select<Color32, string>(<>f__am$cache8).ToList<string>();
        return JsonUtility.ToJson(data);
    }

    public void Refresh()
    {
        if (this.emissionTex == null)
        {
            Model component = base.GetComponent<Model>();
            if (component.instance == null)
            {
                return;
            }
            Renderer componentInChildren = component.instance.GetComponentInChildren<Renderer>();
            if (componentInChildren == null)
            {
                return;
            }
            Material material = componentInChildren.material;
            Texture2D mainTexture = material.mainTexture as Texture2D;
            Texture2D textured2 = null;
            textured2 = mainTexture.ForceInstantiate();
            textured2.filterMode = FilterMode.Point;
            this.mainTexColors = textured2.GetPixels32();
            this.emissionTex = textured2;
            this.emissionTex.SetPixels(Color.black);
            this.blackColors = this.emissionTex.GetPixels32();
            material.SetTexture("_EmissionMap", this.emissionTex);
            material.SetColor("_EmissionColor", Color.white);
            material.EnableKeyword("_EMISSION");
            if (<>f__am$cache6 == null)
            {
                <>f__am$cache6 = x => x;
            }
            if (<>f__am$cache7 == null)
            {
                <>f__am$cache7 = x => x.Key;
            }
            this.groupedMainTexColors = this.mainTexColors.GroupBy<Color32, Color32>(<>f__am$cache6).Select<IGrouping<Color32, Color32>, Color32>(<>f__am$cache7).ToArray<Color32>();
        }
        Color32[] colors = this.blackColors.ToArray<Color32>();
        foreach (Color32 color in this.emissionIndexColors)
        {
            for (int i = 0; i < this.mainTexColors.Length; i++)
            {
                if (this.mainTexColors[i].Equals(color))
                {
                    colors[i] = color;
                }
            }
        }
        this.emissionTex.SetPixels32(colors);
        this.emissionTex.Apply();
    }

    private void Start()
    {
        if (whiteTex == null)
        {
            whiteTex = new Texture2D(1, 1);
        }
        if ((this.emissionIndexColors != null) && (this.emissionIndexColors.Count > 0))
        {
            this.Refresh();
        }
    }

    [Serializable]
    public class Data
    {
        public List<string> emissionIndexHexColors = new List<string>();
    }
}

